package com.sright.bcard.enterprise.web.controller;

import com.sright.bcard.common.service.share.dto.page.PageParamRequest;
import com.sright.bcard.common.service.share.dto.enterprise.AddressDTO;
import com.sright.bcard.common.service.share.dto.enterprise.CompanyDTO;
import com.sright.bcard.enterprise.share.service.AddressService;
import com.sright.bcard.enterprise.share.service.CompanyService;
import com.sright.bcard.common.service.share.mvcresolver.EnableLoginAuth;
import com.sright.common.response.SrightResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 企业公司相关类
 */
@RestController
@RequestMapping("/common")
public class CompanyController extends BaseController{

    @Autowired
    private CompanyService companyService;

    @Autowired
    private AddressService addressService;

    /**
     * 获取公司列表
     * @return
     */
    @GetMapping("/companys.json")
    @EnableLoginAuth
    public SrightResponse companyList(@Validated CompanyDTO companyDTO) {
        buildLoginUserFroCompany(companyDTO);
        return companyService.companyList(companyDTO);
    }

    /**
     * 新增公司
     * @return
     */
    @PostMapping("/companys.json")
    @EnableLoginAuth
    public SrightResponse addCompany(@RequestBody CompanyDTO companyDTO) {
        buildLoginUserFroCompany(companyDTO);
        return companyService.addCompany(companyDTO);
    }

    /**
     * 修改公司
     * @return
     */
    @PutMapping("/companys.json")
    @EnableLoginAuth
    public SrightResponse updateCompany(@RequestBody CompanyDTO companyDTO){
        buildLoginUserFroCompany(companyDTO);
        return companyService.updateCompany(companyDTO);
    }

    /**
     * 删除公司
     * @return
     */
    @DeleteMapping("/companys.json")
    @EnableLoginAuth
    public SrightResponse deleteCompany(@RequestBody CompanyDTO companyDTO) {
        return companyService.deleteCompany(companyDTO.getIds());
    }

    /**
     * 获取企业地址列表
     * @return
     */
    @GetMapping("/address.json")
    @EnableLoginAuth
    public SrightResponse addressList(@Validated PageParamRequest pageParamRequest) {
        pageParamRequest.setEnterpriseId(userContext.getEnterpriseId());
        return addressService.addressList(pageParamRequest);
    }

    /**
     * 新增地址信息
     * @return
     */
    @PostMapping("/address.json")
    @EnableLoginAuth
    public SrightResponse addAddress(@RequestBody AddressDTO addressDTO) {
        buildLoginUserForAddress(addressDTO);
        return addressService.addAddress(addressDTO);
    }

    /**
     * 修改地址信息
     * @return
     */
    @PutMapping("/address.json")
    @EnableLoginAuth
    public SrightResponse updateAddress(@RequestBody AddressDTO addressDTO) {
        buildLoginUserForAddress(addressDTO);
        return addressService.updateAddress(addressDTO);
    }

    /**
     * 删除地址信息
     * @return
     */
    @DeleteMapping("/address.json")
    @EnableLoginAuth
    public SrightResponse deleteAddress(@RequestBody AddressDTO addressDTO) {
        return addressService.deleteAddress(addressDTO.getIds());
    }

    private void buildLoginUserFroCompany(CompanyDTO companyDTO) {
        companyDTO.setEnterpriseId(userContext.getEnterpriseId());
    }

    private void buildLoginUserForAddress(AddressDTO addressDTO) {
        addressDTO.setEnterpriseId(userContext.getEnterpriseId());
        addressDTO.setUserId(userContext.getUserId());
    }

}
